package cn.com.tomato.service;

import cn.com.tomato.mapper.StatisticsMapper;
import cn.com.tomato.mapper.TodoMapper;
import cn.com.tomato.mapper.TodoSetMapper;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: chenlj
 * @CreateTime: 2024-09-03 14:58
 * @Description:
 */
@Service
public class StatisticsService {

    @Autowired
    private StatisticsMapper statisticsMapper;

    public Map<String, Object> getAccumulateData(Long id){
        if(id == -1){
            return statisticsMapper.getAccumulateData(null);
        }
        return statisticsMapper.getAccumulateData(id);
    }


    public Map<String, Object> todayData(Long id){
        Map<String, Object> param = new HashMap<>();
        param.put("start", DateUtil.beginOfDay(new Date()));
        param.put("end", DateUtil.endOfDay(new Date()));
        if(id != -1){
            param.put("id", id);
        }
        return statisticsMapper.todayData(param);
    }

    public List<Map<String, Object>> focusDistribution(JSONObject json){
        // {startDate: "2024-09-10 00:00:00", endDate: "2024-09-10 01:02:06", id: 28}
        Map<String, Object> param = new HashMap<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("startDate = " + simpleDateFormat.format(json.getDate("startDate")));
        System.out.println("endDate = " + simpleDateFormat.format(json.getDate("endDate")));
        param.put("start",  json.getDate("startDate"));
        param.put("end", json.getDate("endDate"));
        if(json.getLong("id") != null && json.getLong("id") != -1) {
            param.put("id", json.getLong("id"));
        }
        return statisticsMapper.focusDistribution(param);
    }

    public List<Map<String, Object>> yearStatistics(JSONObject json){
        Map<String, Object> param = new HashMap<>();
        param.put("start", json.getDate("startDate"));
        param.put("end", json.getDate("endDate"));
        if(json.getLong("id") != null && json.getLong("id") != -1) {
            param.put("id", json.getLong("id"));
        }
        return statisticsMapper.yearStatistics(param);
    }

    public List<Map<String, Object>> monthStatistics(JSONObject json){
        Map<String, Object> param = new HashMap<>();
        param.put("start", json.getDate("startDate"));
        param.put("end", DateUtil.endOfMonth(json.getDate("startDate")));
        if(json.getLong("id") != null && json.getLong("id") != -1) {
            param.put("id", json.getLong("id"));
        }
        return statisticsMapper.monthStatistics(param);
    }

    public List<Map<String, Object>> stopStatistics(JSONObject json){
        Map<String, Object> param = new HashMap<>();
        param.put("start", json.getDate("startDate"));
        param.put("end", DateUtil.endOfMonth(json.getDate("startDate")));
        if(json.getLong("id") != null && json.getLong("id") != -1) {
            param.put("id", json.getLong("id"));
        }
        return statisticsMapper.stopStatistics(param);
    }

}
